package main
import (
	"fmt"
)
func finished() {
	fmt.Println("-------------Finished finding largest-------------")
}
func largest(nums []int) {
	defer finished()	//defer 最后输出
	fmt.Println("Started finding largest")
	max := nums[0]
	for _, v := range nums {
		if v > max {
			max = v
		}
	}
	fmt.Println("Largest number in", nums, "is", max)
}
// 延迟方法
type person struct {
	firstName string
	lastName string
}
func (p person) fullName() {
	fmt.Printf("\n%s %s", p.firstName, p.lastName)
}
// 实参取值（Arguments Evaluation）
func printA(a int) {
	fmt.Println("\nprintA => value of a in deferred function", a)
}

// 29. Defer 含有 defer 语句的函数，会在该函数将要返回之前，调用另一个函数。
func main(){
	nums := []int{12,34,45,56,78,89}
	largest(nums)

	// 延迟方法
	p := person{firstName: "yang", lastName: "hao"}
	defer p.fullName()
	fmt.Printf("Welcome")

	// 实参取值（Arguments Evaluation）
	a1 := 5
	defer printA(a1)
	a1 = 10
	printA(a1)
	fmt.Println("value of a before deferred function call", a1)

	// defer 栈
	// 使用 defer 栈，将一个字符串逆序打印。
	name := "YangHao"
	fmt.Printf("Orignal String: %s\n", string(name))
	fmt.Printf("Reversd String: ")
	for _, v := range []rune(name){
		defer fmt.Printf("%c",v)
	}

	//defer 的实际应用

}
